﻿using Document_Management;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Document_Management.刀具;
namespace Document_Management
{
    public partial class frmCutting_lingyong : Form
    {
        //int iflag = 0;
        static string connectionString = ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        string flag;
        string tb1, tb2,tb3,tb4,tb5,tb6,tb7;
        public frmCutting_lingyong(string flag, string tb1, string tb2, string tb3, string tb4,string tb5,string num,string tb7)
        {
            InitializeComponent();
            this.flag = flag;
            this.tb1 = tb1;
            this.tb2 = tb2;
            this.tb3 = tb3;
            this.tb4 = tb4;
            this.tb5 = tb5;
            this.tb6 = num;
            this.tb7 = tb7;
        }
        //public void Cuttingtool_lingyong_Load()
        //{
        //    //ResetText量具詳細();
        //    SqlConnection con = new SqlConnection(connectionString);
        //    try
        //    {
        //        con.Open();
        //        SqlDataAdapter da = new SqlDataAdapter("sp_刀具訊息_load", con);
        //        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        //        DataTable dt = new DataTable();
        //        da.Fill(dt);
        //        dataGridView1.DataSource = dt;
        //        PublicClass.setRowNumber(dataGridView1);
        //        con.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //        con.Close();
        //    }
        //}
        private void frmCutting_lingyong_Load(object sender, EventArgs e)
        {
            txt刀具編號.Enabled = false;
            txt刀具名稱.Enabled = false;
            txt品名規格.Enabled = false;
            txt規格代號.Enabled = false;
            txt材料編號.Enabled = false;
            txt建檔數量.Enabled = false;
            txt保管人代號.Enabled = false;
            txt刀具編號.Text = tb1;//cb1 1
            txt刀具名稱.Text = tb2; //tb3 2
            txt品名規格.Text = tb3; //tb4 3
            txt規格代號.Text = tb4; //cb2  4
            txt材料編號.Text = tb5; //cb3 5
            txt建檔數量.Text = tb6; //tb4 6
            txt保管人代號.Text = tb7;
        }

        //public void Load_Cuttingtool_lingyong()
        //{
        //    //ResetText量具詳細();
        //    SqlConnection con = new SqlConnection(connectionString);
        //    try
        //    {
        //        con.Open();
        //        SqlDataAdapter da = new SqlDataAdapter("sp_刀具_領用_load", con);
        //        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        //        DataTable dt = new DataTable();
        //        da.Fill(dt);
        //        dataGridView1.DataSource = dt;
        //        PublicClass.setRowNumber(dataGridView1);
        //        con.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //        con.Close();
        //    }
        //}
        private void btn領用_Click(object sender, EventArgs e)
        {
            this.Close();
            frm_cuttinglingyongjilu frm = new frm_cuttinglingyongjilu();
            frm.ShowDialog();
           
        }

        private void btn歸還_Click(object sender, EventArgs e)
        {

        }

        private void btn領用_Click_1(object sender, EventArgs e)
        {
            int check1, check2;
            try
            {
                if (txt領用數量.Text == "")
                {
                    MessageBox.Show("請您輸入領用數量", "錯誤", MessageBoxButtons.OK);
                    txt領用數量.Focus();
                    return;
                }
                if (txt領用人代號.Text == "")
                {
                    MessageBox.Show("請您輸入領用人代號", "錯誤", MessageBoxButtons.OK);
                    txt領用人代號.Focus();
                    return;
                }
                if (cbx領用部門.Text == "")
                {
                    MessageBox.Show("請您輸入領用部門", "錯誤", MessageBoxButtons.OK);
                    cbx領用部門.Focus();
                    return;
                }
                else
                {
                    check1 = Int32.Parse(txt建檔數量.Text.ToString());
                    check2 = Int32.Parse(txt領用數量.Text.ToString());
                    if (check1 < check2)
                    {
                        MessageBox.Show("領用數量越過建檔數量！ 請再輸入！", "錯誤", MessageBoxButtons.OK);
                    }
                    else 
                        if(check2<=0)
                        {
                            MessageBox.Show("不能領用！ 請再檢查！", "錯誤", MessageBoxButtons.OK);
                        }
                    else
                    {
                        con.Open();
                        string sp_insert = "sp_刀具_領用_xinzheng";
                        SqlCommand cmd = new SqlCommand(sp_insert, con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@刀具編號", txt刀具編號.Text.ToString());
                        cmd.Parameters.AddWithValue("@領用日期", (DateTime)dateTime領用日期.Value);
                        cmd.Parameters.AddWithValue("@領用數量", txt領用數量.Text.ToString());
                        cmd.Parameters.AddWithValue("@領用人代號", txt領用人代號.Text.ToString());
                        //cmd.Parameters.AddWithValue("@領用部門", cbx領用部門.Text.ToString());
                        cmd.Parameters.AddWithValue("@備註", txt_ghichu.Text.ToString());
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("領用成功");
                        //Load_Cuttingtool_lingyong();
                        con.Close();
                        this.Close();
                        frm_cuttinglingyongjilu frm = new frm_cuttinglingyongjilu();
                        frm.ShowDialog(); 
                    }
                    

                }
                //bat dau insert vao database


            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btn_離開_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt領用數量_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) || e.KeyChar == '.')
            {
                e.Handled = true;
                MessageBox.Show("很抱歉，請您輸入號碼", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void bt挑選_Click(object sender, EventArgs e)
        {
            String bumen = cbx領用部門.Text.ToString();
            CuttingTool_llingyongren_search frm = new CuttingTool_llingyongren_search(txt領用人代號, bumen);
            frm.ShowDialog();
        }

        private void bt_領用部門_search_Click(object sender, EventArgs e)
        {
            Cuttingtool_manager_seach1 frm = new Cuttingtool_manager_seach1(cbx領用部門);
            frm.ShowDialog();
        }

        private void txt領用人代號_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 刀具領用人 where 刀具領用人代號=N'{0}'", txt領用人代號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_領用人.Text = "";
                else lb_領用人.Text = temp;
                con.Close();
            }
            catch (Exception ex) {
                MessageBox.Show("Error!");
            }
        }

        private void cbx領用部門_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 刀具領用部門 where 刀具領用部門代號=N'{0}'", cbx領用部門.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_領用部門.Text = "";
                else lb_領用部門.Text = temp;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error!");
                con.Close();
            }
        }
    }
}
